﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FallbackConverter.cs" company="Marcin Jastrzebski">
//   Copyright (c) Marcin Jastrzebski (marcin@marcinj.net)
// </copyright>
// <summary>
//   A converter that falls back to the parameter value if value is a null, empty, or whitespace string.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace mPassRT.Converters
{
    using System;

    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Data;

    /// <summary>A converter that falls back to the parameter value if value is a null, empty, or whitespace string.</summary>
    public class FallbackConverter : IValueConverter
    {
        #region Public Methods and Operators

        /// <summary>The convert.</summary>
        /// <param name="value">The value.</param>
        /// <param name="targetType">The target type.</param>
        /// <param name="parameter">The parameter.</param>
        /// <param name="language">The language.</param>
        /// <returns>The <see cref="object"/>.</returns>
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var valueStr = value as string;
            if (targetType == typeof(string))
            {
                return string.IsNullOrWhiteSpace(valueStr) ? parameter : value;
            }

            return DependencyProperty.UnsetValue;
        }

        /// <summary>The convert back.</summary>
        /// <param name="value">The value.</param>
        /// <param name="targetType">The target type.</param>
        /// <param name="parameter">The parameter.</param>
        /// <param name="language">The language.</param>
        /// <returns>The <see cref="object"/>.</returns>
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}